<!---
	Copyright 2010 Edward Trudeau, Jeremy Battle
    This software is freely available under the MIT License: http://www.opensource.org/licenses/mit-license.php
    http://ayc.googlecode.com
    
--->

<cfparam name="url.object" default="" />
<cfparam name="form.rows" default="25" />
<cfparam name="form.sord" default="desc" />
<cfparam name="form.page" default="1" />

<cfif len(trim(url.object))>
<cfset currentObject = entityNew(url.object) />
<cfparam name="form.sidx" default="#url.object#id" />
<cfset formCopy = duplicate(form) />
<cfset structDelete(formCopy,"_search") />
<cfset structDelete(formCopy,"nd") />
<cfset structDelete(formCopy,"page") />
<cfset structDelete(formCopy,"rows") />
<cfset structDelete(formCopy,"sidx") />
<cfset structDelete(formCopy,"sord") />
<cfset structDelete(formCopy,"fieldnames") />

<cfset queryString = "select #url.object#id from #url.object# where 1=1" />
<cfset loopPos = 0 />
<cfloop collection="#formCopy#" item="field">
	<cfset loopPos++ />
	<cfset queryString = queryString & " and #field# LIKE '#formCopy[field]#%'" />
</cfloop>
<cfset queryString = queryString & " ORDER BY #form.sidx# #form.sord#" /> 
<cfset searchObject = ORMExecuteQuery("#queryString#", true, {cacheable=true, maxresults=form.rows, offset="#(form.page-1)*form.rows#"}) />

<cfloop from="1" to="#arrayLen(searchObject)#" index="a">
	<cfif structKeyExists(session,"#url.object#") and structKeyExists(session[url.object],"index_#searchObject[a]#") and DateDiff("n",session[url.object]["index_#searchObject[a]#"].updated,Now()) LT 10>
		<cfoutput>Found in session and doesnt need an update</cfoutput><br />
	<cfelseif structKeyExists(session,"#url.object#") and structKeyExists(session[url.object],"index_#searchObject[a]#") and DateDiff("n",session[url.object]["index_#searchObject[a]#"].updated,Now()) GTE 10>
		<cfoutput>Found in session and needs an update</cfoutput><br />
		<cfset session[url.object]["index_#searchObject[a]#"].object = EntityLoad(url.object, searchObject[a], true) />
		<cfset session[url.object]["index_#searchObject[a]#"].updated = Now() />
	<cfelseif structKeyExists(session,"#url.object#") >
		<cfoutput>#url.object# found in session, but this object wasnt found, adding and the object</cfoutput><br />
		<cfset session[url.object]["index_#searchObject[a]#"] = structNew() />
		<cfset session[url.object]["index_#searchObject[a]#"].object = EntityLoad(url.object, searchObject[a], true) />
		<cfset session[url.object]["index_#searchObject[a]#"].updated = Now() />
	<cfelse>
		<cfoutput>#url.object# not found in session, adding it and the objects</cfoutput><br />
		<cfset session[url.object] = structNew() />
		<cfset session[url.object]["index_#searchObject[a]#"] = structNew() />
		<cfset session[url.object]["index_#searchObject[a]#"].object = EntityLoad(url.object, searchObject[a], true) />
		<cfset session[url.object]["index_#searchObject[a]#"].updated = Now() />
	</cfif>
</cfloop>

<cfcontent reset="true">
<cfoutput>
{	
	"page":#form.page#,
	"total":<cfif currentObject.count() GT 0>#ceiling(currentObject.count()/form.rows)#<cfelse>0</cfif>,
	"records":#currentObject.count()#,
	"rows":[
	<cfif arrayLen(searchObject)>
	<cfloop from="1" to="#arrayLen(searchObject)#" index="x">
		{
		"id":#searchObject[x]#,
		"cell":[
		<cfset listPosition = 0 />
		<cfloop list="#currentObject.getDisplayColumns()#" index="i">
		<cfset listPosition++>
			<cfif compareNoCase(session[url.object]["index_#searchObject[x]#"].object.getORMType(i),"timestamp") EQ 0 OR compareNoCase(session[url.object]["index_#searchObject[x]#"].object.getORMType(i),"datetime") EQ 0>
				"#dateFormat(session[url.object]["index_#searchObject[x]#"].object.get(i),'mm/dd/yyyy')#"
			<cfelse>
				"#replace(session[url.object]["index_#searchObject[x]#"].object.get(i),'"',"\""","all")#"
			</cfif>
			
			<cfif listLen(currentObject.getDisplayColumns()) NEQ listPosition>,</cfif>
		</cfloop>	
		]}<cfif arrayLen(searchObject) NEQ x>,</cfif>
	</cfloop>
	</cfif>
	]
}
</cfoutput>
<!---



<cfdump var="#searchObject#">--->
</cfif>

